package com.dykj.dati.shiro;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
* MyShiroRealm Class
* @author: yc
* @Created: 2021/2/5 14:28
*/
public class MyShiroRealm extends AuthorizingRealm {
    /**角色权限和对应权限添加*/

    @Override
    public AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addRole("user");
        return simpleAuthorizationInfo;
    }
    /**用户认证*/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //加这一步的目的是在Post请求的时候会先进认证，然后在到请求
        if (authenticationToken.getPrincipal() == null) {
            return null;
        }
        //获取用户信息
        String name = authenticationToken.getPrincipal().toString();

        /*QueryWrapper<SysUser> queryWrapper = new QueryWrapper<SysUser>();
        queryWrapper.eq("login_name",name);
        SysUser user=sysUserService.getOne(queryWrapper);
        if (user == null) {
            //这里返回后会报出对应异常
            return null;
        }
        if(user.getIsLock()==1){
            throw new LockedAccountException("账户被锁定");
        }*/

            //这里验证authenticationToken和simpleAuthenticationInfo的信息

          SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(new Object(), "", "");
            return simpleAuthenticationInfo;

    }
    public void clearAuthz(){
        this.clearCachedAuthorizationInfo(SecurityUtils.getSubject().getPrincipals());
    }
}
